public class Pacman {
    public Position pos;
    private MyStack<Position> history = new MyStack<>(100);

    public Pacman(int x, int y) { pos = new Position(x, y); }

    public void move(int dx, int dy) {
        history.push(new Position(pos.x, pos.y));
        pos.x += dx; pos.y += dy;
    }

    public void undo() {
        Position prev = history.pop();
        if (prev != null) pos = prev;
    }
}